
package zj.com.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import org.xutils.DbManager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import zj.com.db.DBUtil;
import zj.com.interfaze.CallData;
import zj.com.interfaze.Callback;
import zj.com.model.Dic;
import zj.com.model.DqrAccompany;
import zj.com.model.JcdInfoDB;
import zj.com.util.Constants;
import zj.com.util.DateUtil;
import zj.com.zj.R;

/**
 * Created by xi on 16/6/3.
 */
public class DqrACompanyAdapter extends BaseAdapter{

    public List<DqrAccompany> list;
    public Context context;
    public LayoutInflater inflater;

    public boolean isEdit = false;
    public Map<Integer,Boolean> selectMap = new HashMap<Integer,Boolean>();
    public int selectPosition = -1;
    public DbManager dbManager;
    public CallData call;

    public DqrACompanyAdapter(List<DqrAccompany> list, Context context){
        dbManager = DBUtil.getInstance(context).getDBManager();
        this.list = list;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = new ViewHolder();
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.adapter_djc_acompany_item, null);
            viewHolder.type_tv = (TextView) convertView.findViewById(R.id.type_tv);
            viewHolder.cb = (CheckBox) convertView.findViewById(R.id.cb);
            viewHolder.name_tv = (TextView) convertView.findViewById(R.id.name_tv);
            viewHolder.des_tv = (TextView) convertView.findViewById(R.id.des_tv);
            viewHolder.jc_type_tv = (TextView) convertView.findViewById(R.id.jc_type_tv);
            viewHolder.date_tv = (TextView) convertView.findViewById(R.id.date_tv);
            viewHolder.ok_tv = (TextView) convertView.findViewById(R.id.ok_tv);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }


       final  DqrAccompany djc = list.get(position);


        viewHolder.name_tv.setText(djc.getInspectionNo());
        viewHolder.des_tv.setText(djc.getOrgName()+"——"+djc.getProjectName());
        String date = djc.getInspectDate();
        viewHolder.date_tv.setText(DateUtil.formatStrimgDate2(date));
        if(isEdit){
            viewHolder.cb.setVisibility(View.VISIBLE);
            viewHolder.type_tv.setVisibility(View.GONE);
        }else{
            viewHolder.type_tv.setVisibility(View.VISIBLE);
            viewHolder.cb.setVisibility(View.GONE);
        }


        String name = djc.getInspectName();
        String type = "";
        try{
            Dic dic = dbManager.selector(Dic.class).where("dic_value", "=", name + "").and("dic_type", "=", Constants.InspectType).findFirst();
            if(dic != null){
                type = dic.getName();
            }
        }catch (Exception e){

        }


        viewHolder.jc_type_tv.setText(type);

        if(type.equals("日常检查"))
        {
            viewHolder.type_tv.setBackgroundResource(R.drawable.rc);
            viewHolder.type_tv.setText("日常");

        }else  if(type.equals("专项检查")){
            viewHolder.type_tv.setBackgroundResource(R.drawable.zx);
            viewHolder.type_tv.setText("专项");

        }else if(type.equals("综合检查"))
        {
            viewHolder.type_tv.setBackgroundResource(R.drawable.zh);
            viewHolder.type_tv.setText("综合");


        }else if(type.equals("其它"))
        {

            viewHolder.type_tv.setBackgroundResource(R.drawable.qt);
            viewHolder.type_tv.setText("其它");


        }
        else if(type.endsWith("节假日检查")){
            viewHolder.type_tv.setBackgroundResource(R.drawable.rc);
            viewHolder.type_tv.setText("节假");
        }


        viewHolder.ok_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(call != null){

                    call.call("",djc.getAccompanyID()+"");
                }
            }
        });


        return convertView;
    }
    private static class ViewHolder {
        TextView type_tv;
        TextView itemTitle;
        CheckBox cb;
        TextView name_tv;
        TextView des_tv;
        TextView jc_type_tv;
        TextView date_tv;
        TextView  ok_tv;

    }

    public void setDataChange(List<DqrAccompany> list){

        this.list = list;
        notifyDataSetChanged();
    }

    public void setIsEdit(boolean isEdit){

        this.isEdit = isEdit;
        notifyDataSetChanged();
    }
    public void setSelect(int position){

        this.selectPosition = position;
        notifyDataSetChanged();
    }

    public void setCallback(CallData call){

        this.call = call;
    }

    public Map<Integer,Boolean> getSelectMap(){

        return selectMap;
    };
}
